﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Bll;
public partial class Users_Upphoto : BasePage
{
    protected string Reurl = configInfo.InstallDir + "Users/";
    protected Cutter cut = new Cutter();
    protected string Action = "";
    protected int id = 0;
    protected UserPhoto UserPhotoBll = new UserPhoto();
    protected string ThumbImg = "";



    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx", "请您登录", true);
        ThumbImg = configInfo.InstallDir + "images/nopic.jpg";
        if (Request["Rurl"] != null)
        {
            Reurl = Server.UrlDecode(Request["Rurl"].ToString());
        }
        if (Request["Action"] != null)
        {
            Action = Request["Action"].ToString();
        }
        if (Request["id"] != null)
        {
            id = StringHelper.isNumber_int(Request["id"].ToString());
        }


        if (!Page.IsPostBack)
        {
            ViewState["Reurl"] = Reurl;//返回路径

            if (Request["Classid"] != null)
            {

                ViewState["Classid"] = Request["Classid"].ToString();
            }

            if (Request["ChannelId"] != null)
            {
                ViewState["ChannelId"] = Request["ChannelId"].ToString();
            }

            if (Request["Types"] != null)
            {
                ViewState["Types"] = Request["Types"].ToString();
            }

            if (Action == "Modify")
            {
                this.Button1.Visible = false;
                this.Button2.Visible = true;
                if (id <= 0)
                {
                    ShowMsgBack("参数错误!");
                    Response.End();
                }

                UserPhoto_Model UserPhotoModel = UserPhotoBll.GetModel(id);
                this.Txt_PhotoName.Text = UserPhotoModel.PhotoName;
                this.Txt_Readme.Text = UserPhotoModel.Readme;
                this.RequiredFieldValidator3.Visible = false;
                ThumbImg = configInfo.InstallDir + UserPhotoModel.PhotoUrl;
            }


        }





        Base_CurrPath = Base_CurrPath + " >> 会员管理中心 >> 照片上传";
        CheckUserExit("Login.Aspx?ReUrl=" + ViewState["Reurl"], "", true);

        cut.UpFoler = configInfo.UpFolder + Base_GetLoginInfo.GetUserID + "/" + ((Channel_Type)StringHelper.isNumber_int(ViewState["ChannelId"].ToString())).ToString() + "/" + "photo_" + ViewState["Types"].ToString() + "_" + ViewState["Classid"].ToString();
        cut.UpSize = configInfo.UpSize;
        cut.IsNewFile = true;




    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserPhoto_Model PhotoModel = new UserPhoto_Model();

        if (ViewState["Classid"] == null)
        {
            ShowMsgBack("参数传递出错！");
            Response.End();
        }
        if (StringHelper.isNumber_int(ViewState["Classid"].ToString()) == 0)
        {
            ShowMsgBack("参数传递出错!");
            Response.End();
        }

        if (ViewState["ChannelId"] == null)
        {
            ShowMsgBack("参数传递出错!");
            Response.End();
        }
        if (ViewState["Types"] == null)
        {
            ShowMsgBack("参数传递出错！");
            Response.End();
        }
        if (StringHelper.isNumber_int(ViewState["Types"].ToString()) == 0)
        {
            ShowMsgBack("参数传递出错!");
            Response.End();
        }

        string Msg = "";
        bool isok = UpLoad_picture.UpSave(FileUpload1, cut, out Msg);
        if (!isok)
        {
            ShowMsgBack(Msg);
            Response.End();
        }
        else
        {
            PhotoModel.PhotoUrl = Msg;
        }
        PhotoModel.ClassId = StringHelper.isNumber_int(ViewState["Classid"].ToString());
        PhotoModel.CreateId = Base_GetUserInfo.UserId;
        PhotoModel.PhotoName = StringHelper.HtmlEncrypt(Txt_PhotoName.Text);
        PhotoModel.IsCheck = (configInfo.Pic_IsCheck == 0) ? 1 : 0;
        PhotoModel.Readme = StringHelper.HtmlEncrypt(Txt_Readme.Text);
        PhotoModel.Types = StringHelper.isNumber_int(ViewState["Types"].ToString());
        PhotoModel.ChannelID = StringHelper.isNumber_int(ViewState["ChannelId"].ToString());
        UserPhoto up = new UserPhoto();
        if (up.Add(PhotoModel) > 0)
        {
            ShowMsgBackParent("图片添加成功！", ViewState["Reurl"].ToString());
            Response.End();
        }



    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ViewState["Classid"] == null)
        {
            ShowMsgBack("参数传递出错！");
            Response.End();
        }
        if (StringHelper.isNumber_int(ViewState["Classid"].ToString()) == 0)
        {
            ShowMsgBack("参数传递出错!");
            Response.End();
        }

        if (ViewState["ChannelId"] == null)
        {
            ShowMsgBack("参数传递出错!");
            Response.End();
        }
        if (ViewState["Types"] == null)
        {
            ShowMsgBack("参数传递出错！");
            Response.End();
        }
        if (StringHelper.isNumber_int(ViewState["Types"].ToString()) == 0)
        {
            ShowMsgBack("参数传递出错!");
            Response.End();
        }



        if (id <= 0)
        {
            ShowMsgBack("参数错误!");
            Response.End();
        }


        UserPhoto_Model photoModel = UserPhotoBll.GetModel(id);//
        UserPhoto_Model Model = new UserPhoto_Model();
        Model.Id = id;
        Model.IsCheck = configInfo.Pic_IsCheck;
        Model.ClassId = StringHelper.isNumber_int(ViewState["Classid"].ToString());
        Model.PhotoName = Txt_PhotoName.Text;
        Model.Readme = StringHelper.RemoveHtml(Txt_Readme.Text);
        if (FileUpload1.FileName != "")
        {
            string Msg = "";
            bool isok = UpLoad_picture.UpSave(FileUpload1, cut, out Msg);

            if (!isok)
            {
                ShowMsgBack(Msg);
                Response.End();
            }
            else
            {
                StringHelper.delFile(photoModel.PhotoUrl);
                Model.PhotoUrl = Msg;
            }
        }
        else
        {
            Model.PhotoUrl = photoModel.PhotoUrl;
        }




        if (UserPhotoBll.Update(Model) > 0)
        {

            ShowMsgBackParent("图片修改成功！", ViewState["Reurl"].ToString());
            Response.End();
        }
        else
        {
            ShowMsgBack("图片修改失败");
            Response.End();
        }


    }
}
